
package node;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * Deletes a node from the system.  
 * 
 * 
 * @author TCSS 558 Team 6
 * @version Spring 2013
 */
public final class NodeDeleter
{

  /**
   * The default time for a node wait for all nodes.
   */
  private static final int SLEEP_TIME = 10000;

  /**
   * 
   * @param the_resistry registry
   * @param id node ID
   */

  private NodeDeleter(final Registry the_resistry, int id)
  {
    try
    {
      final Registry registry = the_resistry;
      final String node_id = "node_id_" + id;
      final String[] registered_nodes = registry.list();
      System.out.println("Node " + node_id +
                         "size of the registry list before deleting a node" +
                         registered_nodes.length);
      // prints the list of registered nodes.
      System.out.println("Node " + node_id +
          "List of registered nodes before deleting the node:");
      for (int i = 0; i < registered_nodes.length; i++)
      {
        System.out.println("Node registered: " + registered_nodes[i]);
      }
      if (registered_nodes.length > 0)
      {
        // there exist other nodes - start talking to them to share network data
        final Node n = (Node) registry.lookup(node_id);
        if (n != null && (n.getMyID() == id))
        {
          System.out.println("Node " + node_id + "Initializing deletion of node : " + id);
          ((Node) n).initializeDataTransfer_Delete();

        }
      }
    }
    catch (final IOException e)
    {
      e.printStackTrace();
    }
    catch (final NotBoundException e)
    {
      System.err.println("The object doesn't bind.");
      System.exit(1);
    }
  }

  /**
   * 
   * Deletes the node from the registry.
   * 
   * @param the_args An array of Strings containing the command line arguments.
   *          The first argument is the host name, the second is the port
   *          number, the third is the registry name, the fourth is the node id
   * 
   */
  public static void main(final String... the_args)
  {
    // get a reference to the remote registry.
    final String host = the_args[0];
    final int port = Integer.parseInt(the_args[1]);
    final String registry_name = the_args[2];
    final int node_id = Integer.parseInt(the_args[3]);

    try
    {
      final Registry bootstrap = LocateRegistry.getRegistry(host, port);
      final Registry registry = (Registry) bootstrap.lookup(registry_name);
      new NodeDeleter(registry, node_id);

    }
    catch (final RemoteException e)
    {
      System.err.println("a problem occurred at remote method call." + e);
      System.exit(1);
    }
    catch (final NotBoundException e)
    {
      System.err.println("The object doesn't bind.");
      System.exit(1);
    }
    catch (final ArrayIndexOutOfBoundsException e)
    {
      System.err.println("Usage:java NodeAdder <host> "
          + "<port> <registry name> <node_id>");
      e.printStackTrace();
    }
  }
}
